﻿function ShowMsg(msg) {
    $.messager.show({
        title: '消息窗口',
        msg: msg,
        timeout: 3000,
        width: 256,
        height: 128,
        showType: 'show'
    });
}
//右下角弹出消息框
function ShowMessage(msg) {
    var PreShowMsg = window.parent.ShowMsg;
    if (PreShowMsg) {
        PreShowMsg(msg);
    } else {
        ShowMsg(msg);
    }
}
//确认-取消弹窗（executeFuc需带一个参数，参数表示是否确认）
function ConfirmMessage(msg, executeFuc) {
    $.messager.confirm('', msg, executeFuc);
}
//显示加载等待
function ShowProcess() {
    $(".datagrid-mask").show();
    $(".datagrid-mask-msg").show();
}
//关闭加载等待
function CloseProcess() {
    $(".datagrid-mask").hide();
    $(".datagrid-mask-msg").hide();
}

//扩展正则表达式验证
$.extend($.fn.validatebox.defaults.rules, {
    validExpression: {
        validator: function (value, param) {
            var re = new RegExp(param[0]);
            return re.test(value);
        },
        message: '正则不匹配'
    }
});

//扩展Combobox空值验证
$.extend($.fn.validatebox.defaults.rules, {
    validCombobox: {
        validator: function (value, param) {
            //return $(param[0] + " option:selected").val() != "";
            return $(param[0]).combobox('getValue') != "";
        },
        message: '请选择一项'
    }
});

//扩展CheckBoxList空值验证
$.extend($.fn.validatebox.defaults.rules, {
    validCheckBoxList: {
        validator: function (value, param) {
            return $(param[0]).find("input[type=hidden]").val() != "";
        },
        message: '请选择一项'
    }
});

//扩展CheckBoxList空值验证
$.extend($.fn.validatebox.defaults.rules, {
    validRadioButtonList: {
        validator: function (value, param) {
            var flag = false;
            $(param[0]).find("input[type=radio]").each(function () {
                if (this.checked) {
                    flag = true;
                    return false;
                }
            });
            return flag;
        },
        message: '请选择一项'
    }
});

//扩展等值验证
$.extend($.fn.validatebox.defaults.rules, {
    validEqual: {
        validator: function (value, param) {
            return $(param[0]).val() == value;
        },
        message: '两次输入的值不一致'
    }

});

/*===表单提交实体Start===*/
var FormEntity = function (selector) {
    this.selector = selector;
    this.source = $(this.selector);
    this.data = this.source.serialize();
    this.formData = this.source.serializeObject();
    
};
FormEntity.prototype =
    {
        //表单选择器
        selector: "",
        //表单提交地址
        url: "",
        //表单参数
        data: {},
        //表单数据实体
        formData: {},
        //表单提交方式
        type: "POST",
        //内容类型
        contentType: "application/x-www-form-urlencoded",
        //默认返回数据为json格式
        dataType: "json",
        //是否异步请求
        async: true,
        //是否缓存
        cache: false,
        //表单提交前事件
        submiting: function () {
            return this.source.form('validate');
        },
        //表单提交失败事件
        submitFailed: function () {
        },
        //表单处理内容，默认Ajax表单提交
        handle: function () {
            var $this = this;
            this.source.form('submit', {
                url: $this.url,
                onSubmit: function () {
                    if (!$this.submiting()) {
                        $this.submitFailed();
                        return false;
                    }
                    ShowProcess();
                    return true;
                },
                success: function (invokeresult) {
                    try {
                        invokeresult = StringToJson(invokeresult);
                        $this.submitSuccess(invokeresult);
                        $this.submited();
                        if ($this.isClearData) {
                            $this.clearFormData();
                        }
                    } catch (e) {
                        ShowMessage(invokeresult);
                    }
                    CloseProcess();
                }
            });
        },
        //表单提交
        submit: function () {
            this.handle();
        },
        //表单提交成功事件
        submitSuccess: function (invokeresult) {
        },
        //表单提交完成事件
        submited: function () {
        },
        //是否清楚表单数据
        isClearData: true,
        //清除表单数据
        clearFormData: function () {
            this.source.form('clear');
        }
    };
/*===表单提交实体End===*/

//远程加载下拉项
function LoadDropDownList(selector, selectedValue, remoteUrl) {
    var $DropDownList = $(selector);
    $(selector + "_script").remove();//页面加载的时候加载了一次，$.datagrid加载了工具栏的时候又加载了一次，导致脚本重复执行，所以在此移除脚本
    var objAjaxEntity = new AjaxEntity();
    objAjaxEntity.url = remoteUrl;
    objAjaxEntity.executeSuccess = function (InvokeResult) {
        if (InvokeResult.ResultCode == "Success") {
            var options = "";
            $.each(InvokeResult.Data, function (i, item) {
                options += "<option " + (selectedValue == item.Value ? "selected=\"selected\" " : "") + "value=\"" + item.Value + "\">" + item.Text + "</option>";
            });
            $DropDownList.append(options);
            $DropDownList.validatebox();
        }
    };
    objAjaxEntity.execute();
}

//远程加载CheckBox选项
function LoadCheckBoxList(selector, selectedValue, isValid, remoteUrl) {
    var values = selectedValue.split(",");
    var objAjaxEntity = new AjaxEntity();
    objAjaxEntity.url = remoteUrl;
    objAjaxEntity.executeSuccess = function (InvokeResult) {
        if (InvokeResult.ResultCode == "Success") {
            var options = "";
            $.each(InvokeResult.Data, function (i, item) {
                options += "<input type=\"checkbox\" onclick=\"SetCheckValue(this);\" value=\"" + item.Value + "\" " + ($.inArray(item.Value, values) >= 0 ? "checked=\"checked\" " : "") + (isValid ? "class=\"easyui-validatebox\" data-options=\"validType:'validCheckBoxList[&quot;" + selector + "&quot;]'\" " : "") + "/>" + item.Text;
            });
            var $CheckBoxList = $(selector);
            $CheckBoxList.append(options);
        }
    };
    objAjaxEntity.execute();
}

//远程加载Radio选项
function LoadRadioList(selector, name, selectedValue, isValid, remoteUrl) {
    var objAjaxEntity = new AjaxEntity();
    objAjaxEntity.url = remoteUrl;
    objAjaxEntity.executeSuccess = function (InvokeResult) {
        if (InvokeResult.ResultCode == "Success") {
            var options = "";
            $.each(InvokeResult.Data, function (i, item) {
                options += "<input type=\"radio\" name=\"" + name + "\" value=\"" + item.Value + "\" " + (selectedValue == item.Value ? "checked=\"checked\" " : "") + (isValid ? "class=\"easyui-validatebox\" data-options=\"validType:'validRadioButtonList[&quot;" + selector + "&quot;]'\" " : "") + "/>" + item.Text;
            });
            var $RadioList = $(selector);
            $RadioList.append(options);
        }
    };
    objAjaxEntity.execute();
}

//关闭目标Dom下的弹窗
function CloseiFrameDialog(selector, target) {
    target.$(selector).dialog("close");
}